import { HOST_ZIMO_POINT_CONFIG, HOST_RONG_POINT_CONFIG, GUEST_ZIMO_POINT_CONFIG, GUEST_RONG_POINT_CONFIG } from '../configs/POINTS'

// 是否满贯
function isMangan(time, mark) {
  if (time === 'three' && Number(mark) >= 70) {
    return true;
  } else if (time === 'four' && Number(mark) >= 40) {
    return true;
  }
  return time === 'slam';
}

// 亲家自摸基础变化分数
function getCustomChnagePoint(time, mark, changePointSets) {
  if (isMangan(time, mark)) {
    return changePointSets[0]
  } else if (['one', 'two', 'three', 'four'].includes(time)) {
    return changePointSets[1][time][mark]
  } else if (time === 'over') {
    return changePointSets[2]
  } else if (time === 'double') {
    return changePointSets[3]
  } else if (time === 'triple') {
    return changePointSets[4]
  } else {
    return changePointSets[5]
  }
}

// 亲家自摸基础变化分数
function getHostZimoChnagePoint(time, mark) {
  return getCustomChnagePoint(time, mark, [4000, HOST_ZIMO_POINT_CONFIG, 6000, 8000, 12000, 16000])
}

// 亲家荣和变化点数
function getHostRongChangePoint(time, mark) {
  return getCustomChnagePoint(time, mark, [12000, HOST_RONG_POINT_CONFIG, 18000, 24000, 32000, 48000])
}

// 子家自摸变化点数
function getGuestZimoChangePoint(time, mark) {
  return getCustomChnagePoint(time, mark, [[4000, 2000], GUEST_ZIMO_POINT_CONFIG, [6000, 3000], [8000, 4000], [12000, 6000], [16000, 8000]])
}

// 子家荣和变化点数
function getGuestRongChangePoint(time, mark) {
  return getCustomChnagePoint(time, mark, [8000, GUEST_RONG_POINT_CONFIG, 12000, 16000, 24000, 32000])
}

// 立直点数变化
function getReachChangePoints(record) {
  const reachGamers = record.reachGamers;
  const changePoints = [0, 1, 2, 3].map(index => {
    return reachGamers.includes(index) ? -1000 : 0
  });
  return changePoints;
}

// 计算罚符点数变化
function getLiuChangePoints(record) {
  const tingGamers = record.tingGamers;
  let losePoint = 0;
  let getPoint = 0;
  if (tingGamers.length === 1) {
    [losePoint, getPoint] = [1000, 3000]
  } else if (tingGamers.length === 2) {
    losePoint = getPoint = 1500
  } else if (tingGamers.length === 3) {
    [losePoint, getPoint] = [3000, 1000]
  }
  const changePoints = [0, 1, 2, 3].map(index => {
    if (tingGamers.includes(index)) {
      return getPoint
    }
    return -losePoint
  })
  return changePoints
}

function calcPoints(record) {
  const reachChangePoints = getReachChangePoints(record);
  let changePoints = new Array(4).fill(0);
  let stickNum = 0;
  const { winGamer, result, huMethod, byte, round, time, mark, loseGamer, reachGamers } = record;
  // 和局
  if (result === 'hu') {
    // 亲家和牌
    if (winGamer + 1 === round) {
      // 自摸
      if (huMethod === 'zimo') {
        // 查亲家自摸点数
        let changePoint = getHostZimoChnagePoint(time, mark)
        changePoint += byte * 100;
        changePoints.fill(0 - changePoint);
        changePoints[winGamer] = changePoint * 3;
      }
      // 荣和
      else {
        // 查亲家荣和点数
        let changePoint = getHostRongChangePoint(time, mark)
        changePoint += byte * 300;
        changePoints[winGamer] = changePoint;
        changePoints[loseGamer] = -changePoint;
      }
    }
    // 子家和牌
    else {
      // 自摸
      if (huMethod === 'zimo') {
        const [hostChangePoint, guestChangePoint] = getGuestZimoChangePoint(time, mark)
        changePoints.fill(- guestChangePoint - byte * 100);
        changePoints[round - 1] = - hostChangePoint - byte * 100;
        changePoints[winGamer] = guestChangePoint * 2 + hostChangePoint + byte * 300;
      }
      // 荣和
      else {
        // 查子家荣和点数
        const changePoint = getGuestRongChangePoint(time, mark) + byte * 300;
        changePoints[winGamer] = changePoint;
        changePoints[loseGamer] = -changePoint;
      }
    }
    changePoints[winGamer] += reachGamers.length * 1000;
  }
  // 流局
  else if (result === 'liu') {
    changePoints = getLiuChangePoints(record);
    stickNum = reachGamers.length;
  }

  changePoints.forEach((point, i) => {
    changePoints[i] = point + reachChangePoints[i]
  })

  return { changePoints, stickNum };
}

function calcFinalPoints(records) {
  let leftStickNum = 0;
  let finalPoints = new Array(4).fill(25000);
  records.forEach(record => {
    const { changePoints, stickNum } = calcPoints(record)
    changePoints.forEach((point, i) => {
      finalPoints[i] = finalPoints[i] + point;
    })
    leftStickNum += stickNum;
    if (record.result === 'hu') {
      finalPoints[record.winGamer] += leftStickNum * 1000;
      leftStickNum = 0;
    }
  })
  return { finalPoints, leftStickNum }
}

module.exports = {
  calcPoints,
  calcFinalPoints,
};
